package com.nky.controller;

import cn.hutool.core.util.StrUtil;
import com.nky.common.Result;
import com.nky.common.constant.StatusCode;
import com.nky.controller.dto.UserDTO;
import com.nky.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@Tag(name = "用户模块")
public class UserController {

    @Resource
    private IUserService userService;

    @GetMapping("/hello")
    public String hello() {
        System.out.println("hello");
        return "hello";
    }

    /**
     * 用户登录，对登录信息进行校验
     *
     * @param userDTO 前端传来的用户信息，自动包装为用户对象
     * @return 登录校验结果
     */
    @Operation(summary = "用户登录",description = "校验用户输入的信息并登录")
    @PostMapping("/login")
    public Result loginAccount(@RequestBody UserDTO userDTO) {
        System.out.println("收到登录请求");
        String username = userDTO.getUsername();
        String password = userDTO.getPassword();
        System.out.println("name: " + username + " password: " + password);
        // 校验name和password是否为空
        if (StrUtil.isBlank(username) || StrUtil.isBlank(password)) {
            System.out.println("用户名或密码为空");
            return Result.error(StatusCode.PARAM_ERROR.getCode(), StatusCode.PARAM_ERROR.getMsg());
        }
        // 在service层验证密码是否正确，并赋予相应的菜单权限
        UserDTO dto = userService.loginAccount(userDTO);
        return Result.success(dto);
    }
}
